<form id="form1" method="post">
	<input name="id" id="id" class="mini-hidden" value="0"/>
	<div style="padding-left:11px;padding-bottom:5px;">
		<table style="table-layout:fixed;width:100%;">
			<tr style="height:25px;">
				<td style="width:50%;height:400px;" colspan="2">
					<div id="datagrid1" idField="id" showFooter="false" class="mini-datagrid" style="width:100%;height:100%;" url="/staff/role">
					    <div property="columns">
					        <div type="checkcolumn" headerAlign="center">选择</div>
					        <div name="name" field="name" width="100px">权限角色名称<input property="editor" class="mini-textbox" style="width:100%;"/></div>
					        <div name="desc" field="desc" width="150px">备注<input property="editor" class="mini-textarea" emptyText="请输入备注" style="width:100%;height:22px;"/></div>
					        <div name="action" width="80" headerAlign="center" align="center" renderer="onActionRend" cellStyle="padding:0;">操作</div>
					    </div>
					</div>
				</td>
				<td style="width:50%;height:400px;" rowspan="3">
					<div id="treegrid1" showCheckBox="true" autoCheckParent="true" checkRecursive="true" class="mini-treegrid" style="width:100%;height:100%;" url="/staff/menuAll" showTreeIcon="false" treeColumn="name" idField="id" parentField="parent" resultAsTree="false" allowSelect="true">
					    <div property="columns">
					        <div type="indexcolumn"></div>
					        <div name="name" field="name" width="250px">组织机构名称</div>
					    </div>
					</div>
				</td>
			</tr>
		</table>
	</div>
	<div style="text-align:center;padding:10px;">   
		<a class="mini-button" onClick="newRow()" style="width:100px;margin-right:20px;">增加角色</a>
		<a class="mini-button" onClick="RoleUpd()" style="width:100px;margin-right:20px;">修改权限</a>
	</div>
</form>
<script type="text/javascript">
mini.parse();
var grid = mini.get("datagrid1");
var treegrid = mini.get("treegrid1");
var urlDel = "/staff/roleDel";
var urlUpd = "/staff/roleUpd?id=0";
grid.load();
treegrid.expandAll();

//行选择设定功能状态
grid.on("selectionchanged",function(e){
	var row = grid.getSelected();
	if (row != null) {
		if (row.id != undefined){
			urlUpd = "/staff/roleUpd?id=" + row.id;
			treegrid.setValue(row.permissions);
		}else{
			urlUpd = "/staff/roleUpd?id=0";
		}
	}
});

//编辑记录模版
function onActionRend(e) {
    var grid = e.sender;
    var record = e.record;
    var uid = record._uid;
    var rowIndex = e.rowIndex;

    var s = ' <a class="RowButton" href="javascript:editRow(\'' + uid + '\')">编辑</a>'
          + ' <a class="RowButton" href="javascript:roledelRow(\'' + uid + '\')">删除</a>';

    if (grid.isEditingRow(record)) {
        s = ' <a class="RowButton" href="javascript:updateRow(\'' + uid + '\')"> 提交 </a>'
          + ' <a class="RowButton" href="javascript:cancelRow(\'' + uid + '\')"> 取消 </a>'
    }
    return s;
}

//删除数据
function roledelRow(row_uid) {
    var row = grid.getRowByUID(row_uid);
    if (row) {
    	parent.$.dooDialog.confirm("你确定要删除吗？",function(){
            grid.loading("删除中，请稍后......");
            $.ajax({
            	type:"POST",
                url:urlDel,
                data:{id:row.id},
                cache:false,
                dataType: "json",
                success:function(text){
                    if(text.code=="MG"){
                    	parent.$.dooDialog.alert(text.msg);
                    }else if(text.code=="NG"){
                    	parent.$.dooDialog.alert("角色删除失败！");
                    }
                    grid.reload();
                },
                error:function(){}
            });
        });
    }
}

//修改角色权限
function RoleUpd(row_uid){
	var row = grid.getSelected();
    if (row) {
    	parent.$.dooDialog.confirm("你确定要修改权限吗？",function(){
            $.ajax({
            	type:"POST",
                url:"/staff/roleSave",
                data:{id:row.id,role:treegrid.getValue()},
                cache:false,
                dataType: "json",
                success:function(text){
                    if(text.code=="OK"){
                    	grid.reload();
                    	parent.$.dooDialog.alert("修改成功！");
                    }else{
                    	parent.$.dooDialog.alert("修改失败！");
                    }
                },
                error:function(){}
            });
        });
    }else{
    	parent.$.dooDialog.alert("请选择一个角色！");
    }
}
</script>